package dk.itu.samd.group2.tagapastryClient;

import java.io.IOException;

import org.junit.After;

import dk.itu.samd.group2.tagapastry.TagapastryImpl;

public class GlobalTestEnvironment {
	
	protected DeliciousData dd = new DeliciousData();
	protected TagapastryImpl impl;
	protected MessageCollector mc;
	
	protected long startTime, endTime;
	protected int retThreshold, expThreshold, numOfNodes;
	
	protected void setUp(int nodes, int ret, int exp) {
		TestDataContainer.CreateNodes(nodes,ret,exp);
		impl = TestDataContainer.getApps().get(0);
		this.mc = TestDataContainer.getMsgCollector();
		startTimer();
	}
	
	@After
	public void tearDown() {
		stopTimer(true);
	}
	
	protected void waitForBuffer(MessageCollector mc) {
		while(!mc.isBufferEmpty() /*|| !mc.dataReceived()*/) {
			sleep(0);
			System.out.println(String.format("Messages still in progress: %d",mc.getCounter()));
		}
	}
	
	protected boolean loadUsingNewsDataSet(TagapastryImpl impl, String object, int num) {
		boolean result = true;
		for(int i = 0; i <= num; i++) {
			result &= impl.tag(object,"URLS",String.format("%s%s.com", object,(char)('a'+i)));
		}
		
		return result;
	}
		
	protected void sleep(int sleep) {
		try {
			System.out.println("I'm done with loading data.");
			if(sleep == 0)
				sleep = 1000;
			
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	protected void lockTest() {
		try {
			System.out.println("Locking...");
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	void startTimer() {
		System.out.println("Started loading: " + new java.util.Date());
		startTime = System.currentTimeMillis();
	}
	
	void stopTimer(boolean print) {
		endTime = System.currentTimeMillis();
		if(print)
			printTime();
	}
	
	void printTime() {
		System.out.println(String.format("Query took %f seconds", (float)(endTime-startTime)/1000));
	}
	
	
}
